package cn.luoshaocheng.order.controller;

import cn.luoshaocheng.basic.util.LoginContext;
import cn.luoshaocheng.order.dto.OrderDto;
import cn.luoshaocheng.order.service.IOrderPetAcquisitionService;
import cn.luoshaocheng.order.domain.OrderPetAcquisition;
import cn.luoshaocheng.order.query.OrderPetAcquisitionQuery;
import cn.luoshaocheng.basic.util.jsonResult;
import cn.luoshaocheng.basic.util.PageList;
import cn.luoshaocheng.user.domain.Logininfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/orderPetAcquisition")
public class OrderPetAcquisitionController {

    @Autowired
    public IOrderPetAcquisitionService orderPetAcquisitionService;

    /**
     * 接口：添加或修改
     * @param orderPetAcquisition  传递的实体
     * @return jsonResult 响应给前端
     */
    @PutMapping
    public jsonResult addAndUpdate(@RequestBody OrderPetAcquisition orderPetAcquisition){
        try {
            if( orderPetAcquisition.getId()!=null)
                orderPetAcquisitionService.update(orderPetAcquisition);
            else
                orderPetAcquisitionService.save(orderPetAcquisition);
            return jsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("系统异常,请稍后重试!");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return jsonResult 响应给前端
    */
    @ApiOperation(value = "取消订单接口" )
    @DeleteMapping(value="/{id}")
    public jsonResult cancel(@PathVariable("id") Long id){
        try {
            orderPetAcquisitionService.cancel(id);
            return jsonResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return jsonResult.me().setMsg("系统异常,请稍后重试!");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return jsonResult 响应给前端
    */
    @PatchMapping
    public jsonResult patchDel(@RequestBody List<Long> ids){
        try {
                orderPetAcquisitionService.patchDel(ids);
            return jsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("系统异常,请稍后重试!");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public OrderPetAcquisition loadById(@PathVariable("id")Long id) {
        return orderPetAcquisitionService.loadById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<OrderPetAcquisition> loadAll(){
        return orderPetAcquisitionService.loadAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<OrderPetAcquisition> queryPage(@RequestBody OrderPetAcquisitionQuery query, HttpServletRequest request) {

        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            return orderPetAcquisitionService.queryPage(query,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<OrderPetAcquisition>();
        }
    }
    @PostMapping("/confirm")
    public jsonResult confirm(@RequestBody OrderDto dto) {
        try {
            orderPetAcquisitionService.confirm(dto);
            return jsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setResultObj("订单提交失败！");
        }
    }

}
